package sources;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

/**
 * A class that runs the Add ToDo GUI and behind the scene code.
 *
 * @author Alana Meditz
 * @author Ty Yago
 * @author Katherine Kishaba
 * @author Jimmy CUmming
 * 
 */

public class AddToDoFrame extends AddComponentFrame {
	
	private ICalendar iCal;
	
	private JLabel classificationL, commentL, priorityL, locationL, resourcesL, dtstartL, summaryL;
	private JTextField commentTF, locationTF, resourcesTF, dtstartTF, summaryTF;
	private JComboBox classificationCB, priorityCB;
	private JButton addB, backB;
	private AddButtonHandler addBH;
	private BackButtonHandler backBH;
	
	/**
	 * AddToDoFrame constructor.
	 * Creates the GUI allowing users to ToDo components to the given ICalendar object. 
	 * @param iCal The ICalendar to which ToDo components will be added.
	 */
	public AddToDoFrame(ICalendar iCal) {
		this.iCal = iCal;
		
		setTitle("Add ToDo");
		setUpFrame();
		
		instantiateComboBoxes();
		instantiateLabels();
		instantiateTextFields();
		instantiateButtons();

		setUpPane();
	}
	
	/**
	 * Helper function used by constructor to instantiate JComboBox objects
	 */
	private void instantiateComboBoxes() {
		classificationCB = new JComboBox(classifications);
		classificationCB.setEditable(true);
		
		priorityCB = new JComboBox(priorities);
		priorityCB.setEditable(false);
	}
	
	/**
	 * Helper function used by constructor to label input boxes.
	 */
	private void instantiateLabels() {
		classificationL = new JLabel("Enter classification: ", SwingConstants.RIGHT);
		commentL = new JLabel("Enter comment: ", SwingConstants.RIGHT);
		priorityL = new JLabel("Select priority: ", SwingConstants.RIGHT);
		locationL = new JLabel("Enter location: ", SwingConstants.RIGHT);
		resourcesL = new JLabel("Enter resources: ", SwingConstants.RIGHT);
		dtstartL = new JLabel("Enter start date & time (yyyymmdd-hhmmss)*: ", SwingConstants.RIGHT);
		summaryL = new JLabel("Enter summary: ", SwingConstants.RIGHT);
	}
	
	/**
	 * Helper function used by constructor to create text fields for user input.
	 */
	private void instantiateTextFields() {
		commentTF = new JTextField(30);
		locationTF = new JTextField(30);
		resourcesTF = new JTextField(30);
		dtstartTF = new JTextField(30);
		summaryTF = new JTextField(30);
	}
	
	/**
	 * Helper function used by constructor to create buttons and implement their event handlers.
	 */
	private void instantiateButtons() {
		addB = new JButton("Add");
		addBH = new AddButtonHandler();
		addB.addActionListener(addBH);
		backB = new JButton("Back");
		backBH = new BackButtonHandler();
		backB.addActionListener(backBH);
	}
	
	/**
	 * Helper function used by constructor to declare pane properties and add Frame elements to the
	 * ContentPane of this frame for display.
	 */
	private void setUpPane() {
		Container pane = getContentPane();
		pane.setLayout(new GridLayout(8, 2));
		
		// Add items to pane
		pane.add(summaryL);
		pane.add(summaryTF);
		pane.add(classificationL);
		pane.add(classificationCB);
		pane.add(commentL);
		pane.add(commentTF);
		pane.add(priorityL);
		pane.add(priorityCB);
		pane.add(locationL);
		pane.add(locationTF);
		pane.add(resourcesL);
		pane.add(resourcesTF);
		pane.add(dtstartL);
		pane.add(dtstartTF);
		pane.add(addB);
		pane.add(backB);
	}
	
	/**
	 * 
	 * Event handler for the Add Button.
	 *
	 */
	private class AddButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String classification, comment, priority, location, resources, dtstart, summary;
			
			classification = (String) classificationCB.getSelectedItem();
			comment = commentTF.getText();
			priority = (String) priorityCB.getSelectedItem();
			location = locationTF.getText();
			resources = resourcesTF.getText();
			dtstart = dtstartTF.getText();
			summary = summaryTF.getText();
			
			ICalComponent component = new ICalComponent("VTODO");
			if(!classification.equals("")) {
				component.addComponentProperty("CLASS", classification);
			}
			
			if(!comment.equals("")) {
				component.addComponentProperty("COMMENT", comment);
			}
			
			if(!priority.equals("")) {
				component.addComponentProperty("PRIORITY", priority);
			}
			
			if(!location.equals("")) {
				component.addComponentProperty("LOCATION", location);
			}
			
			if(!resources.equals("")) {
				component.addComponentProperty("RESOURCES", resources);
			}
			
			if(!dtstart.equals("")) {
				if(!checkDateFormat(dtstart)) {
					ErrorFrame errorFrame = new ErrorFrame("Incorrect date format!");
					return;
				}
				
				dtstart = dtstart + "Z";
				dtstart = dtstart.replace('-', 'T');
				component.addComponentProperty("DTSTART", dtstart);
			} else {
				ErrorFrame errorFrame = new ErrorFrame("Start date must be provided!");
				return;
			}
			
			if(!summary.equals("")) {
				component.addComponentProperty("SUMMARY", summary);
			}
			
			iCal.addComponent(component);
		}
	}
}